package com.zhangdi.dwweb.exception;

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zhangdi on 2020/01/17 09:49:41.
 */

@Component
public class ExceptionHandler extends DefaultErrorAttributes {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        return super.resolveException(request, response, handler, ex);
        //TODO log
    }

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> defaultError = super.getErrorAttributes(webRequest, includeStackTrace);
        Map<String, Object> error = new LinkedHashMap<>();
        error.put("code", defaultError.get("status"));
        error.put("msg", defaultError.get("error"));
        error.put("url", defaultError.get("path"));
        return error;
    }
}
